package com.dm.cloud.images.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectVolumeResponse;
import com.dm.cloud.core.exception.CustomException;
import com.dm.cloud.images.common.po.ImagesEndpoint;
import com.dm.cloud.images.mode.VolumeCreateRequest;
import com.dm.cloud.images.mode.VolumeDetail;
import com.dm.cloud.images.service.ImagesEndpointService;
import com.dm.cloud.images.service.VolumesService;
import com.dm.cloud.images.util.DockerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据卷配置
 */
@Service
public class VolumesServiceImpl implements VolumesService {

    @Autowired
    private ImagesEndpointService imagesEndpointService;

    @Override
    public VolumeDetail create(VolumeCreateRequest volumeCreateRequest) {
        if(StrUtil.isEmpty(volumeCreateRequest.getName())){
            throw new CustomException("数据卷名称不能为空！");
        }
        if(StrUtil.isEmpty(volumeCreateRequest.getNodeId())){
            throw new CustomException("创建的节点不能为空！");
        }
        ImagesEndpoint endPointPo = imagesEndpointService.getById(volumeCreateRequest.getNodeId());
        if(endPointPo==null || ObjectUtil.isEmpty(endPointPo)){
            throw new CustomException("节点不存在！");
        }
        DockerClient dockerClient = null;
        try {
            dockerClient = DockerUtil.get(endPointPo.getHost(), endPointPo.getDockerPort());
            InspectVolumeResponse inspect = DockerUtil.createVolumes(dockerClient, volumeCreateRequest);
            VolumeDetail volume = VolumeDetail.buildByInspect(inspect, endPointPo.getId(), endPointPo.getName(), endPointPo.getHost());
            return volume;
        }finally {
            try {
                dockerClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public List<VolumeDetail> list(String name,String nodeId) {
        List<VolumeDetail> reList=new ArrayList<>();
        List<ImagesEndpoint> endpoints=new ArrayList<>();
        if(StrUtil.isEmpty(nodeId)){
            endpoints = imagesEndpointService.list("state", 1);
        }else{
            ImagesEndpoint endpoint = imagesEndpointService.getById(nodeId);
            endpoints.add(endpoint);
        }
        if(endpoints!=null){
            for (ImagesEndpoint endpoint : endpoints) {
                DockerClient dockerClient = null;
                try {
                    dockerClient = DockerUtil.get(endpoint.getHost(), endpoint.getDockerPort());
                    List<InspectVolumeResponse> vs = DockerUtil.volumesList(dockerClient, name);
                    for (InspectVolumeResponse v : vs) {
                        VolumeDetail volume = VolumeDetail.buildByInspect(v, endpoint.getId(), endpoint.getName(), endpoint.getHost());
                        reList.add(volume);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    try {
                        dockerClient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return reList;
    }

    @Override
    public Boolean delete(String nodeId, String name) {
        if(StrUtil.isEmpty(name)){
            throw new CustomException("要删除的数据卷不能为空！");
        }
        if(StrUtil.isEmpty(nodeId)){
            throw new CustomException("创建的节点不能为空！");
        }
        ImagesEndpoint endPointPo = imagesEndpointService.getById(nodeId);
        if(endPointPo==null || ObjectUtil.isEmpty(endPointPo)){
            throw new CustomException("节点不存在！");
        }
        DockerClient dockerClient = null;
        try {
            dockerClient = DockerUtil.get(endPointPo.getHost(), endPointPo.getDockerPort());
            return DockerUtil.deleteVolume(dockerClient,name);
        }finally {
            try {
                dockerClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
